<?php
/**
 *
 * 专辑相关模型
 * xinhua
 *
 *
 */
class ml_model_dbCollectionBox extends Lib_datamodel_db
{

    public function __construct()
    {
        $db_config = ml_factory::load_standard_conf('db');

        parent::__construct('collection_box' , $db_config['collection_box']);

    }

    public function createCollection($uid, $cname, $description)
    {
        if(!$this->init_db($uid, self::DB_MASTER))
        return false;

        $arrInsert = array(
            'uid' => $uid,
            'name' => $cname,
            'description' => $description,
            'status' => '1',
            'cip' => Tool_ip::get_real_ip(),
            'col_info'=>null,
            'cnt_col'=>0,
            'cnt_fans'=>0,
            'ctime'=> date('Y-m-d H:i:s', time())
        );

        return $this->insert($arrInsert);
    }
    public function delCollection($uid,$col_id){
        if(!$this->init_db($uid, self::DB_MASTER))
        return false;

        return $this->update(array('status' => 9), "`col_id`='$col_id' and `uid`='$uid'", 1);
    }

    public function editCollection($uid,$col_id,$name,$descrip){
        if(!$this->init_db($uid, self::DB_MASTER))
        return false;

        return $this->update(array('name' => $name,'description'=>$descrip), "`col_id`='$col_id' and `uid`='$uid'", 1);
    }

    public function beforeRepost($uid){
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $uid=$this->escape($uid);

        $sql="select name,col_id from `$this->table` where `uid`='$uid' and `status`='1' order by `ctime` desc";

        $rs=$this->fetch($sql);

        return $rs? $this->_data: false;
    }

    public function getColinfo($uid,$col_id){
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $col_id=$this->escape($col_id);

        $sql="select `col_info`,`name` from `$this->table` where `col_id`='$col_id' and `uid`='$uid' and `status`='1'";

        $rs=$this->fetch($sql);
        $this->_data[0]['col_info']=unserialize($this->_data[0]['col_info']);
        return $rs?$this->_data[0]:false;

    }

    public function submitRepost($uid,$col_id,$col_info){

        if(!$this->init_db($uid, self::DB_MASTER))
        return false;
        $str = serialize($col_info);
        $sql="update `$this->table` set `cnt_col`=`cnt_col`+1,`col_info`='$str' where `col_id`='$col_id' and `uid`='$uid'";
        return  $this->query($sql);

    }

    public function updateCol_info($uid,$col_id,$col_info){
        if(!$this->init_db($uid, self::DB_MASTER))
        return false;
        $str = serialize($col_info);
        $sql="update `$this->table` set `col_info`='$str' where `col_id`='$col_id' and `uid`='$uid'";
        return  $this->query($sql);
    }
    /**
     *
     * 删除转采的宝贝，采集-1
     * xinhua
     *
     *
     */
    public function delContent($uid,$col_id){

        if(!$this->init_db($uid, self::DB_MASTER))
        return false;
        $col_id = $this->escape($col_id);
        $sql="update `$this->table` set `cnt_col`=`cnt_col`-1 where `col_id`='$col_id' and `uid`='$uid'";
        return  $this->query($sql);

    }
    /**
     *
     * 点击我的专辑
     * xinhua
     *
     *
     */
    public function getListByUid($uid){
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;
        $sql="select * from `$this->table` where `uid`='$uid' and `status`='1' order by `ctime` desc";

        $rs=$this->fetch($sql);

        return $rs?$this->_data:false;
    }
    /**
     *
     * 更多专辑
     * xinhua
     *
     *
     */
    public function getMoreCol($uid,$col_id){
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;
        $sql="select * from `$this->table` where `uid`='$uid' and `col_id`!='$col_id' and `status`='1'
         and `cnt_col`!='0' order by `cnt_col` desc limit 5";//转采数排，最多5个，空的不显示

        $rs=$this->fetch($sql);

        return $rs?$this->_data:false;
    }
    public function getDescrip($uid,$cid){
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;
        $cid=$this->escape($cid);
        $sql="select `name`,`description`,`cnt_fans`,`cnt_col` from `$this->table` where `col_id`='$cid'";

        $rs=$this->fetch($sql);

        return $rs?$this->_data[0]:false;

    }
    /**
     *
     * 查看专辑数是否满了（20）
     * xinhua
     *
     *
     */
    public function yz_full($uid){
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;
        $sql="select count(*) as `num` from `$this->table` where `uid`='$uid' and `status`='1'";

        $rs=$this->fetch($sql);

        return $rs?$this->_data[0]:false;
            
    }

}

?>